1 Project Description


The data was collected from Gallup World Poll. Their survey consisted of questions that asked participants to rank their own life on a Cantril ladder with a scale from 1 to 10, 10 being the best ideal way of living and 0 being the worst. This data set focuses on the happiness score of each country, which ranges from 0 to 10. Each country is ranked based on that averaged happiness score for participants. The team recorded scores for these factors: economy or GDP per Capita, family or social support, health or life expectancy, and freedom to help explain the happiness score of each country.


library(socviz)
library(lubridate)
library(geofacet)
library(ggthemes)
library(ggrepel)
library(ggridges)
library(plyr)
library(skimr)
library(tidyverse)
library(gganimate)
library(plotly)
library(stargazer)  # regression tables
library(ggstatsplot)
library(corrr)
theme_set(theme_classic())



2 Data Wrangling



2.1 Happiness Data

# Read 2015 Data
h15 <- read_csv("Happiness_Data/2015.csv")
h15 <- h15 %>%
  dplyr::mutate(Year = 2015) %>%
  dplyr::rename(H_rank=`Happiness Rank`, # Modify variable names
                H_score = `Happiness Score`,
                GDP=`Economy (GDP per Capita)`,
                Health=`Health (Life Expectancy)`,
                Trust=`Trust (Government Corruption)`,
                SE=`Standard Error`,
                dystopia_res = `Dystopia Residual`) 


# Read 2016 Data
h16 <- read_csv("Happiness_Data/2016.csv")  
h16 <- h16 %>%
  dplyr::mutate(Year = 2016,
      `Standard Error` = (`Upper Confidence Interval`-`Lower Confidence Interval`)/3.92) %>%
              # SE = (upper limit – lower limit) / 3.92. 
              # This is for 95% CI
  dplyr::select(-c(`Upper Confidence Interval`,`Lower Confidence Interval`)) %>%
  dplyr::rename(H_rank=`Happiness Rank`, # Modify variable names
                H_score = `Happiness Score`,
                GDP=`Economy (GDP per Capita)`,
                Health=`Health (Life Expectancy)`,
                Trust=`Trust (Government Corruption)`,
                SE=`Standard Error`,
                dystopia_res = `Dystopia Residual`)



# Since we don't have a variable 'Region' starting from 2017, we will create it for 
# each year
h_regions <- dplyr::select(h16, Country, Region)



# Read 2017 Data
h17 <- read_csv("Happiness_Data/2017.csv")  
h17 <- h17 %>%
  dplyr::mutate(Year = 2017,
                `Standard Error` = (`Whisker.high`-`Whisker.low`)/3.92,) %>%
  merge(h_regions,by="Country", all.x=T) %>%
  dplyr::select(-c(`Whisker.high`,`Whisker.low`)) %>%
  dplyr::rename(H_rank=`Happiness.Rank`, # Modify variable names
                H_score = Happiness.Score,
                GDP=Economy..GDP.per.Capita.,
                Health=Health..Life.Expectancy.,
                Trust=Trust..Government.Corruption.,
                SE=`Standard Error`,
                dystopia_res = Dystopia.Residual)


# Read 2018 Data
h18 <- read_csv("Happiness_Data/2018.csv")  
h18 <- h18 %>%
  dplyr::mutate(Year = 2018) %>%
  dplyr::rename(H_rank=`Overall rank`, # Modify variable names
                H_score = `Score`,
                GDP=`GDP per capita`,
                Country = `Country or region`,
                Health=`Healthy life expectancy`,
                Trust=`Perceptions of corruption`,
                Freedom = `Freedom to make life choices`,
                Family = `Social support`) %>%
  merge(h_regions,by="Country", all.x=T) %>%
  dplyr::mutate(dystopia_res = H_score - (GDP + Family + Health + Freedom + Generosity + as.numeric(Trust)))



# Read 2019 Data
h19 <- read_csv("Happiness_Data/2019.csv")  
h19 <- h19 %>%
  dplyr::mutate(Year = 2019) %>%
  dplyr::rename(H_rank=`Overall rank`, # Modify variable names
                H_score = `Score`,
                GDP=`GDP per capita`,
                Country = `Country or region`,
                Health=`Healthy life expectancy`,
                Trust=`Perceptions of corruption`,
                Freedom = `Freedom to make life choices`,
                Family = `Social support`) %>%
  merge(h_regions,by="Country", all.x=T) %>%
  dplyr::mutate(dystopia_res = H_score - 
                  (GDP + Family + Health + Freedom + Generosity + as.numeric(Trust)))

# Combine all data into all_dat
h_alldat <- tibble(rbind.fill(h15,h16,h17,h18,h19))
h_alldat <- h_alldat %>%
  dplyr::mutate(Country = as.factor(tolower(Country)),
                Region = as.factor(Region))

#rmarkdown::paged_table(h_alldat)
save(h_alldat, file = 'h_alldat.RData')
knitr::kable(papeR::summarize_numeric(h_alldat, type = "numeric", group = "Region",variables = c("H_rank"),  test = FALSE))
Region N Mean SD Min Q1 Median Q3 Max
1 H_rank Australia and New Zealand 10 9.10 1.10 8 8.0 9.0 10.0 11
1.1 Central and Eastern Europe 144 75.63 26.80 20 55.5 73.0 91.5 138
1.2 Eastern Asia 28 66.32 22.54 25 52.0 65.0 83.5 101
1.3 Latin America and Caribbean 109 49.75 29.97 12 28.0 43.0 63.0 148
1.4 Middle East and Northern Africa 96 79.56 41.40 11 39.0 83.0 109.0 156
1.5 North America 10 11.30 5.14 5 7.0 11.0 15.0 19
1.6 Southeastern Asia 44 80.55 35.45 22 46.5 81.5 107.0 145
1.7 Southern Asia 35 112.46 23.31 67 97.0 115.0 127.5 154
1.8 Sub-Saharan Africa 185 126.86 21.41 55 114.0 131.0 143.0 158
1.9 Western Europe 103 26.12 26.36 1 6.0 17.0 36.0 102


2.2 MERGE: Death and Risk Factors Data & Happiness Data

# Read data in
death_dat <- read_csv('/Volumes/Programming/Spring 2022/DANL 310/my_website/aLin-96.github.io/Happiness_Data/number-of-deaths-by-risk-factor.csv')

death_dat <- death_dat %>%
  filter(Year > 2015) %>%
  arrange(Year)

rmarkdown::paged_table(death_dat)


2.3 MERGE: Country Profile UN Data & Happiness Data

country_profile <- read_csv('/Volumes/Programming/Spring 2022/DANL 310/my_website/aLin-96.github.io/Happiness_Data/kiva_country_profile_variables.csv')

country_profile <- country_profile %>%
  select(-c(`GDP per capita (current US$)`)) %>%
  dplyr::mutate(country = tolower(country)) %>%
  dplyr::rename(Country = country,
                Life_expectancy = `Life expectancy at birth (females/males, years)`,
                Urban_pop = `Urban population (% of total population)`,
                Phone_subscriptions = `Mobile-cellular subscriptions (per 100 inhabitants)...41`,
                Employment_rate = `Employment: Services (% of employed)`,
                GVA_services = `Economy: Services and other activity (% of GVA)`,
                Infant_mortality = `Infant mortality rate (per 1000 live births`,
                Age_distribution = `Population age distribution (0-14 / 60+ years, %)`,
                Fertility_rate = `Fertility rate, total (live births per woman)`,
                Sanitation_facilities = `Pop. using improved sanitation facilities (urban/rural, %)`,
                Urban_pop_growthrate = `Urban population growth rate (average annual %)`,
                GVA_agriculture = `Economy: Agriculture (% of GVA)`,
                Pop_growthRate = `Population growth rate (average annual %)`,
                Energy_production = `Energy production, primary (Petajoules)`
) %>%
  separate(Life_expectancy, c('Life_expectancy_F','Life_expectancy_M'), sep = "/") %>%
  separate(Age_distribution, c('Age_distribution_below14','Age_distribution_above60'), sep = "/") %>%
  dplyr::select(-c(Region)) %>%
  mutate(Life_expectancy_F = as.numeric(Life_expectancy_F),
         Life_expectancy_M = as.numeric(Life_expectancy_M),
         Life_expectancy_F = if_else(Life_expectancy_F < mean(Life_expectancy_F),
                                     "Under Average",
                                     "Above Average"),
         Life_expectancy_M = if_else(Life_expectancy_M < mean(Life_expectancy_M),
                                     "Under Average",
                                     "Above Average")) # Change the Life_expectancy variables into categorical variables
  

h_p_alldat <- merge(h_alldat, country_profile, by = "Country")

rmarkdown::paged_table(country_profile)



Find Meaningful Variables related to Happiness Score

Top 10 Positive & Negative Correlation Coefficients

h_p_corr <- data.matrix(h_p_alldat, rownames.force = NA) %>%
    correlate() %>% 
    stretch() %>% 
    filter(x != y & x == "H_score" & 
             y != "H_rank" & 
             y != "Net Official Development Assist. received (% of GNI)") %>%
    arrange(desc(r))

# Top 10 Positive Correlation Coefficients
h_p_corr_positive10 <- h_p_corr %>%
  head(10)
# Top 10 Negative Correlation Coefficients
h_p_corr_negative10 <- h_p_corr %>%
  arrange(r) %>%
  head(10)


Top 10 Positive Correlation Coefficients


Top 10 Negative Correlation Coefficients


2.4 TOP 10 AVG Hppiness Scores (2015 ~ 2019)

# Get Top 10 mean of happiness rank from 2015 ~ 2019

top_10 <- h_alldat %>%
  group_by(Country) %>%
  dplyr::summarise(mean_rank = mean(H_rank)) %>%
  arrange(desc(mean_rank)) %>%
  filter(mean_rank <= 10)

rmarkdown::paged_table(top_10)






3 Visualizations



3.1 Boxplot of Happiness Scores in different Regions

ggplot(dplyr::filter(h_alldat, Region != "NA")) +
  geom_boxplot(aes(x = H_score, y=reorder(Region, H_score), color = Region))+
  theme_classic() +
  theme(legend.position = "None") +
  labs(x = "Happiness Scores", y = "Regions")


3.2 Happiness Scores vs GDP

ggplot(dplyr::filter(h_alldat, Region != "NA"), aes(x = GDP, y=H_score, color = Region)) +
  geom_point() +
  theme_classic()+
  labs(title = "Happiness Scores vs GDP by Region\n")


base <- h_alldat %>%
  plot_ly(x = ~GDP, y = ~H_score, 
          text = ~Country, hoverinfo = "text",
          width = 800, height = 500, size = 2) 

base %>%
  add_markers(color = ~Region, frame = ~Year, ids = ~Country) %>%
  animation_opts(1000, easing = "elastic", redraw = FALSE) %>%
  animation_slider(
    currentvalue = list(prefix = "YEAR ", font = list(color="red"))
  ) 


3.3 World Map by Happiness Scores

world_map <- map_data("world")
world <- world_map %>%
  dplyr::rename(Country = region) %>%
  dplyr::mutate(Country = str_to_lower(Country),
         Country = ifelse(
            Country == "usa",
            "united states", Country),
         Country = ifelse(
            Country == "democratic republic of the congo",
            "congo (kinshasa)", Country),
         Country = ifelse(
            Country == "republic of congo",
            "congo (brazzaville)", Country),
         Country = as.factor(Country))

h_alldat_world <- left_join(h_alldat, world, by = "Country",all.x=TRUE)

p <- ggplot(h_alldat_world, aes(long, lat, group = group,
                                fill = H_score,
                                frame = Year))+
  geom_polygon(na.rm = TRUE)+
  scale_fill_gradient(low = "white", high = "#FD8104", na.value = NA) +
  theme_map()

p %>%
  plotly::ggplotly() %>%
  animation_opts(1000, easing = "elastic",transition = 0,  redraw = FALSE)



4 Regressions


country_formula <- H_score ~ GDP + Family + Health + Freedom + Generosity
country_model <- lm(country_formula, data = h_p_alldat)

stargazer(country_model, type = "html", omit = c("Constant"))
Dependent variable:
H_score
GDP 1.429***
(0.147)
Family 0.470***
(0.123)
Health 0.993***
(0.210)
Freedom 1.665***
(0.246)
Generosity 0.855***
(0.304)
Observations 351
R2 0.670
Adjusted R2 0.665
Residual Std. Error 0.593 (df = 345)
F Statistic 140.142*** (df = 5; 345)
Note: p<0.1; p<0.05; p<0.01




4.1 Noticable Relationships: GDP & Freedom

colors <- c("Fredom" = "red", "GDP" = "blue")

ggplot(data = h_alldat)+
  geom_smooth(aes(x = Freedom, y = H_score, color = 'Freedom'), method = "lm")+
  geom_point(aes(x = Freedom, y = H_score, color = 'Freedom'), alpha = .3)+
geom_smooth(aes(x = GDP, y = H_score, color = 'GDP',), method = "lm")+
  geom_point(aes(x = GDP, y = H_score, color = 'GDP'), alpha = .3)+
  labs(title = "Noticable Relationships",
       subtitle = "Dataset: Happiness",
       x = "Explanatory Variables")

formula = H_score ~ GDP*Freedom
model = lm(formula, data = h_alldat)
summary(model)
## 
## Call:
## lm(formula = formula, data = h_alldat)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.32037 -0.39225  0.05136  0.41110  1.61633 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   3.1431     0.1400  22.452  < 2e-16 ***
## GDP           1.3314     0.1573   8.464  < 2e-16 ***
## Freedom       1.1986     0.3426   3.499 0.000494 ***
## GDP:Freedom   1.3162     0.3505   3.756 0.000186 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5995 on 778 degrees of freedom
## Multiple R-squared:  0.7183, Adjusted R-squared:  0.7173 
## F-statistic: 661.4 on 3 and 778 DF,  p-value: < 2.2e-16
Formula = H_score ~ Urban_pop + Life_expectancy_F + Life_expectancy_M + 
  Phone_subscriptions + Employment_rate + GVA_services + Infant_mortality + 
  Fertility_rate + Sanitation_facilities + Urban_pop_growthrate + GVA_agriculture + 
  Energy_production

model <- lm(Formula, data = h_p_alldat)
summary(model)
## 
## Call:
## lm(formula = Formula, data = h_p_alldat)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.47867 -0.35734  0.02989  0.43786  1.44703 
## 
## Coefficients:
##                                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                     3.240409   0.356509   9.089  < 2e-16 ***
## Urban_pop                       0.012919   0.002921   4.424 1.31e-05 ***
## Life_expectancy_FUnder Average -0.788456   0.141144  -5.586 4.78e-08 ***
## Life_expectancy_MUnder Average -0.380368   0.133390  -2.852  0.00462 ** 
## Phone_subscriptions             0.003335   0.002866   1.163  0.24551    
## Employment_rate                 0.005454   0.002301   2.370  0.01835 *  
## GVA_services                    0.017583   0.004586   3.834  0.00015 ***
## Infant_mortality                0.019042   0.003826   4.977 1.03e-06 ***
## Fertility_rate                 -0.171238   0.058658  -2.919  0.00374 ** 
## Sanitation_facilities          -0.011116   0.002481  -4.480 1.02e-05 ***
## Urban_pop_growthrate            0.042034   0.035978   1.168  0.24349    
## GVA_agriculture                 0.006917   0.004054   1.706  0.08890 .  
## Energy_production              -0.001051   0.001255  -0.837  0.40306    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5945 on 338 degrees of freedom
## Multiple R-squared:  0.6755, Adjusted R-squared:  0.664 
## F-statistic: 58.64 on 12 and 338 DF,  p-value: < 2.2e-16